package com.nifostasky.comic.common.mybatisplusconfig;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.nifostasky.comic.common.threadlocal.SessionLoad;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * mybatisPlus自动填充配置
 */
@Component
public class AutoFillDataHandler implements MetaObjectHandler {

    /**
     * 插入时,自动填充数据
     * @param metaObject
     */
    @Override
    public void insertFill(MetaObject metaObject) {
        //创建时间
        this.strictInsertFill(metaObject,"createDate", Date.class,new Date());
        //创建用户
        Long userId = SessionLoad.getLocalData().getUserId();
        this.strictInsertFill(metaObject,"createUser",String.class, String.valueOf(SessionLoad.getLocalData().getUserId()));
    }

    /**
     * 更新时自动填充数据
     * @param metaObject
     */
    @Override
    public void updateFill(MetaObject metaObject) {
        //修改用户
        this.strictInsertFill(metaObject,"updateUser",String.class,String.valueOf(SessionLoad.getLocalData().getUserId()));
        //更新时间
        this.strictInsertFill(metaObject,"updateDate", Date.class,new Date());
    }
}
